package study.distributed.shardingjdbc.service;

import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import study.distributed.shardingjdbc.config.BaseDataSourceConfig;
import study.distributed.shardingjdbc.dao.sharding.OrderItemMapper;
import study.distributed.shardingjdbc.dao.sharding.OrderMapper;
import study.distributed.shardingjdbc.model.sharding.OrderItemModel;
import study.distributed.shardingjdbc.model.sharding.OrderModel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class OrderService {
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    public void insertList() {
        for (int i = 1; i < 5; i++) {
            OrderModel orderModel = new OrderModel();
            orderModel.setCreateTime(new Date());
            orderModel.setUserId(12);
            orderMapper.insert(orderModel);
            log.info("orderId:"+orderModel.getOrderId());

            List<OrderItemModel> itemModels = new ArrayList<OrderItemModel>();
            for (int j = 1; j < 5; j++) {
                OrderItemModel itemModel = new OrderItemModel();
                itemModel.setOrderId(orderModel.getOrderId());
                itemModel.setName("商品"+j);
                itemModels.add(itemModel);
            }
            orderItemMapper.insertList(itemModels);
        }
    }

    public OrderModel getUserOrder() {
       return orderMapper.getUserOrder(2,27);
    }

    public void insertGenKey() {
        OrderItemModel itemModel = new OrderItemModel();
        itemModel.setOrderId(1L);
        itemModel.setName("商品");
        orderItemMapper.insert(itemModel);
    }

    public List<OrderItemModel> getUserOrderItem() {
        return orderItemMapper.getUserOrderItem(4,418402066709348352L);
    }


    @Transactional(rollbackFor = Exception.class,transactionManager = BaseDataSourceConfig.SHARDING_TRANSACTION_MANAGER)
    @ShardingTransactionType(value = TransactionType.XA)
    public void insertListWithTransaction() {
        for (int i = 1; i < 5; i++) {
            OrderModel orderModel = new OrderModel();
            orderModel.setCreateTime(new Date());
            orderModel.setUserId(11);
            orderMapper.insert(orderModel);
            log.info("orderId:"+orderModel.getOrderId());

            List<OrderItemModel> itemModels = new ArrayList<OrderItemModel>();
            for (int j = 1; j < 5; j++) {
                OrderItemModel itemModel = new OrderItemModel();
                itemModel.setOrderId(orderModel.getOrderId());
                itemModel.setName("商品"+j);
                itemModels.add(itemModel);
            }
            orderItemMapper.insertList(itemModels);
        }
        int i = 1/0;
    }
}
